package com.fantuan.mail;

import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 * 邮件服务类
 * 
 * @author wangxinning
 * 
 */
@Service
public class MailService {

	@Autowired
	@Qualifier("mailSender")
	private JavaMailSender mailSender;

	@Autowired
	@Qualifier("velocityEngine")
	private VelocityEngine velocityEngine;

	public void sendEmail(Map<String, Object> model, final String subject,
			final String vmfile, final String[] mailTo, final String[] files) {
		// 生成邮件内容
		String text = VelocityEngineUtils.mergeTemplateIntoString(
				velocityEngine, vmfile, "GBK", model);

		// 发送邮件
		// mailSender.send(preparator);//发送邮件
		SimpleMailMessage mail = new SimpleMailMessage();

		try {
			mail.setTo(mailTo);// 接受者
			mail.setFrom("message@staff.5dfantuan.com");// 发送者,这里还可以另起Email别名，不用和xml里的username一致
			mail.setSubject(subject);// 主题
			mail.setText(text);// 邮件内容
			mailSender.send(mail);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}